package operation;
import Book.Book;
import Book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation {
    public void work(BookList bookList){
        System.out.println("删除图书...");
        System.out.println("请输入你要删除的图书名：");
        Scanner scanner=new Scanner(System.in);
        String bookName=scanner.nextLine();
        int currentsize= bookList.getUsedSize();
        int pos=-1,i=1;
        for(;i<currentsize;i++){
            Book book=bookList.getBook(i);
            if(book.getName().equals(bookName)){
                pos=i;
                break;
            }
        }
        if(i>=currentsize){
            System.out.println("没有你要找的书：");
            return;
        }

        for(int j=pos;j<currentsize-1;j++){
            Book book=bookList.getBook(j+1);
            bookList.setBook(j,book);
        }
        bookList.setBook(currentsize-1,null);
        bookList.setUsedSize(currentsize-1);
        System.out.println("删除图书成功：");

    }
}
